<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('巡检日期')" prop="inspectionTime">
            <el-date-picker
              v-model="form.inspectionTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('巡检日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('巡检人员')" prop="userId">
            <UserSelect v-model="form.userId" :placeholder="$t('巡检人员')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('巡检情况')" prop="inspectionCase">
            <el-input v-model="form.inspectionCase" type="textarea" :placeholder="$t('巡检情况')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="deal_inspection" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="deal:inspection" :btn-hide="btnHide" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
    import { saveDealInspection, getDealInspection } from '@/api/deal/estate/inspection/dealInspection'
    import { getDealName } from '@/api/base/deal/dealBase'
    export default {
        data() {
            return {
              // 菜单标识
              menuKey: 'deal_inspection',
              // 是否隐藏其它按钮
              otherBtnHide: false,
              // 是否隐藏保存按钮
              btnHide: false,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    inspectionTime: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }, { validator: this.verityTime }
                    ],
                    userId: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    inspectionCase: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true
            }
        },
        created() {
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealInspection(data.id).then(response => {
                      debugger
                        this.form = response.data
                      if (response.data.actStatus !== 'D' && response.data.actStatus !== 'W') {
                        this.buttonShow = true
                      }
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                     dealId: this.$route.query.id,
                    actTitle: undefined,

                    inspectionTime: new Date(),

                    userId: this.$store.state.user.userId,

                    inspectionCase: undefined,

                    actId: undefined,

                    actStatus: undefined,

                    createBy: undefined,

                    createTime: undefined,

                    updateBy: undefined,

                    updateTime: undefined,

                    delFlag: undefined,
                  fileList: []

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                    this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          async verityTime(rule, value, callback) {
            if (this.form.inspectionTime != null) {
              var aData = new Date()
              if (new Date(this.form.inspectionTime).getTime() > new Date(aData).getTime()) {
                callback(new Error(this.$t('巡检日期不可比当前日期晚!')))
              } else {
                callback()
              }
            } else {
              callback()
            }
          },
          submitForm: function(type) {
            return new Promise((resolve, reject) => {
              this.$refs['form'].validate(valid => {
                // 验证数据
                if (valid) {
                  this.loading = true
                  // 保存数据
                  saveDealInspection(this.form).then(response => {
                    if (response.code === 200) {
                      if (type === 'process') {
                        // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                        this.form.id = response.data
                        // 启动流程
                        this.startProcess(response.data)
                      } else {
                        this.msgSuccess(this.$t('保存成功'))
                        this.closeForm()
                      }
                      resolve()
                    } else {
                      this.msgError(response.msg)
                      this.loading = false
                      reject()
                    }
                  }).catch(() => {
                    this.loading = false
                  })
                } else {
                  reject()
                  this.locationError(document)
                }
              })
            })
          },
          // 启动流程
          startProcess: async function(dataId) {
            var dealName = await getDealName(this.form.dealId)
            this.loading = false
            // 启动流程时候传入的参数
            const processData = {}
            // 待办标题
            processData.title = '' + '项目巡检' + '' + '-' + '' + dealName + ''
            // 业务表名
            processData.dataTable = 'deal_inspection'
            // 业务ID
            processData.dataId = dataId
            // 业务表单地址(待办页面需要打开的页面)
            processData.formPath = '@/views/deal/estate/inspection/dealInspectionForm'
            processData.viewPath = '@/views/deal/estate/inspection/dealInspectionView'
            // 菜单标识(根据菜单标识判断的走哪个流程)
            processData.menuKey = this.menuKey
            this.startProcessByMenu(processData, () => {
              this.form.actStatus = 'D'
              this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
              this.closeForm()
            })
          }
        }
    }
</script>
